package com.hrt.manage.comments.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.Comments;
import com.hrt.bean.HRTMember;
import com.hrt.manage.comments.service.CommentsService;
import com.hrt.manage.member.service.HRTMemberService;
import com.hrt.manage.product.service.GiftDetailService;


@Controller
@RequestMapping("manage/comments")
public class CommentsController extends BaseController<Comments>{
	
	//评论
	@Autowired
	private CommentsService commentsService;
	//礼品规格
	@Autowired
	private GiftDetailService giftDetailService;
	//会员
	@Autowired
	private HRTMemberService hRTMemberService;
	
	/**分页*/
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/queryByPage")
	@ResponseBody
	public Map queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request) {
		Map resultMap = new HashMap();
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);
		setPageSize(pageSize);
		setTotalCount(commentsService.totalCount());
		List<Comments> resultList = commentsService.queryByPage(
				getPageIndex(), getPageSize());
		setList(resultList);
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));
		page = this.getPage();
		resultMap.put("pageResult", page);
		return resultMap;
	}

	/**
	 * 查看
	 * 
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpServletRequest request) {
		ModelAndView view = new ModelAndView();

		list = commentsService.queryByPage(getPageIndex(), getPageSize());
		setTotalCount(commentsService.totalCount());
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));
		page = this.getPage();
		//会员
		List<HRTMember> hrtList = hRTMemberService.searchDelHRTMember();
		view.addObject("hrtList", hrtList);
		view.addObject("pageResult", page);
		return view;

	}
	
	/**删除*/
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Long[] delete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletResponse response) throws IOException {
		Long[] idArr = null;
		if (ids != null) {
			if (ids.indexOf(",") != -1) {
				String[] idStrs = ids.split(",");
				idArr = new Long[idStrs.length];
				for (int idx = 0; idx < idStrs.length; idx++) {
					idArr[idx] = Long.parseLong(idStrs[idx]);
				}
			} else {
				idArr = new Long[] { Long.parseLong(ids) };
			}
		}
		try {
			commentsService.deleteById(idArr);

			return idArr;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}
	
	/**
	 * 添加
	 * */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void save(@ModelAttribute("comments") Comments comm,
			HttpServletRequest request, HttpServletResponse response) {
		
		try {
			HRTMember hrtMe = this.hRTMemberService.getMemberByid(comm.getHrtMemberId());
			comm.setHrtMember(hrtMe);
			comm.setHrtMemberName(hrtMe.getMbrName());
			comm.setGiftDetail(this.giftDetailService.getById(Long.parseLong("2")));
			commentsService.addComments(comm);
			response.sendRedirect("list.do");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * 跳转到修改页面
	 * */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/show")
	@ResponseBody
	public Map show(@RequestParam(value = "id", required = true) String commId,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();
		if (commId != null && !"".equals(commId)) {
			Comments comm = this.commentsService.getById(new Long(commId));
			if (comm != null) {
				map = transBean2Map(comm);
			}
		}
		return map;
	}

	/**
	 * 修改
	 * */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public void update(@ModelAttribute("comments") Comments comm,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			if(comm.getHrtMemberId()>0){
				HRTMember hrtMe = this.hRTMemberService.getMemberByid(comm.getHrtMemberId());
				comm.setHrtMember(hrtMe);
				comm.setHrtMemberName(hrtMe.getMbrName());
			}else{
				comm.setHrtMember(null);
			}
			comm.setGiftDetail(this.giftDetailService.getById(comm.getGdeId()));
//			comm.setHrtMember(this.hRTMemberService.getMemberByid(comm.getHrtMemberId()));
			commentsService.updateComments(comm);
			response.sendRedirect("list.do");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
